package com.gray.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gray.pojo.Article;
import com.gray.pojo.User;
import com.gray.pojo.UserComment;
import com.gray.service.impl.UserCommentService;

@Controller
@RequestMapping(value="/comment")
public class CommentController {
	
	@Autowired
	private UserCommentService userCommentService;
	
	
	/**
	 * 返回 评论列表
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getcomment")
	public Map getComment(@RequestParam(value="articleId",required=true)int articleId ){
		Map map = new HashMap();
		List list = userCommentService.getUserCommentByArticleId((short)articleId);
		map.put("comment", list);
		return map;
		
	}
	
	
	/**
	 * 提交评论
	 * @param request
	 * @param commentContent
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="comitcomment")
	public int comitComment(HttpServletRequest request,
			@RequestParam(required=true) String commentContent,
			@RequestParam(required=true) int articleId){
		User user = (User)request.getSession().getAttribute("user");
		UserComment userComment = new UserComment();
		Article article = new Article();
		if(user==null){
			user = new User();
			user.setUserId(3);//游客
		}
		article.setArticleId((short)articleId);
		userComment.setArticle(article);
		userComment.setUser(user);
		userComment.setCommitTime(new Date());
		userComment.setCommitContent(commentContent);
		userComment.setCommitIp(request.getRemoteAddr());
		
		return userCommentService.commitComment(userComment);
	}
	
	@ResponseBody
	@RequestMapping(value="commitpraise")
	public int commitPraise(@RequestParam(required=true)int cId){
		UserComment userComment = new UserComment();
		userComment.setcId(cId);
		return userCommentService.updatePraise(userComment);
	}
	
}
